// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UnityServiceLocatorAdapter.cs" company="">
//   
// </copyright>
// <summary>
//   Defines a
//   &lt;seealso cref="IUnityContainer" /&gt;
//   adapter for
//   the &lt;see cref="IServiceLocator" /&gt; interface
//   to be used by the Composite Application Library.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;

namespace Web
{
    /// <summary>
    /// Defines a 
    /// <seealso cref="IUnityContainer"/>
    /// adapter for
    /// the <see cref="IServiceLocator"/> interface
    /// to be used by the Composite Application Library.
    /// </summary>
    public class UnityServiceLocatorAdapter : ServiceLocatorImplBase
    {
        /// <summary>
        /// unity container.
        /// </summary>
        private readonly IUnityContainer unityContainer;

        /// <summary>
        /// Initializes a new instance of the <see cref="UnityServiceLocatorAdapter"/> class. 
        /// Initializes a new instance of <see cref="UnityServiceLocatorAdapter"/>.
        /// </summary>
        /// <param name="unityContainer">
        /// The 
        /// <seealso cref="IUnityContainer"/>
        /// that will be used
        /// by the <see cref="DoGetInstance"/> and <see cref="DoGetAllInstances"/> methods.
        /// </param>
        [CLSCompliant(false)]
        public UnityServiceLocatorAdapter(IUnityContainer unityContainer)
        {
            this.unityContainer = unityContainer;
        }

        /// <summary>
        /// Resolves all the instances of the requested service.
        /// </summary>
        /// <param name="serviceType">
        /// LayoutType of service requested.
        /// </param>
        /// <returns>
        /// Sequence of service instance objects.
        /// </returns>
        protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
        {
            return unityContainer.ResolveAll(serviceType);
        }

        /// <summary>
        /// Resolves the instance of the requested service.
        /// </summary>
        /// <param name="serviceType">
        /// LayoutType of instance requested.
        /// </param>
        /// <param name="key">
        /// Name of registered service you want. May be null.
        /// </param>
        /// <returns>
        /// The requested service instance.
        /// </returns>
        protected override object DoGetInstance(Type serviceType, string key)
        {
            return unityContainer.Resolve(serviceType, key);
        }
    }
}